#include "PestaniaConfCliente.h"

PestaniaConfCliente::PestaniaConfCliente(const std::string& rute)
:PestaniaProperties(rute),boton_act("Actualizar"),boton_conect("Conectar")
{
    //creo el archivo si es la primera vez que se corre el programa
    if ( this->abmPro.is_empty() )
    {
        this->abmPro.Escribir("Usuario","");
        this->abmPro.Escribir("Password","");
        this->abmPro.Escribir("IP","");
        this->abmPro.Escribir("Puerto","");
        this->abmPro.Escribir("IntervaloPolling","");
        this->abmPro.Escribir("DirectorioAsincronizar","");
        this->abmPro.VolcarFile();
        this->VistaPro.ActualizarVista();
    }

    this->boton_act.signal_clicked().connect(sigc::mem_fun(*this,&PestaniaConfCliente::on_boton_act_clickeado));
    this->boton_conect.signal_clicked().connect(sigc::mem_fun(*this,&PestaniaConfCliente::on_boton_conect_clickeado));
    //***********************************************
    this->add(this->vbox_vtot);
    this->add(this->boton_act);
    this->add(this->boton_conect);
    //***********************************************
}

bool PestaniaConfCliente::vc()
{
    bool us= this->abmPro.IsFilled("Usuario");
    bool pa= this->abmPro.IsFilled("Password");
    bool ip= this->abmPro.IsFilled("IP");
    bool pu= this->abmPro.IsFilled("Puerto");
    bool ii= this->abmPro.IsFilled("IntervaloPolling");
    bool ds= this->abmPro.IsFilled("DirectorioAsincronizar");
    return ( us && pa && pu && ii && ds && ip );
}

void PestaniaConfCliente::crearCarp()
{
    bool dir = this->abmPro.SeEncuentra("DirectorioAsincronizar");
    if ( dir )
    {
        std::string rute = this->abmPro.GetValueFromKey("DirectorioAsincronizar");
        mkdir( rute.c_str(), 0777 );
        std::string RutaIndice = rute + "indice";
        std::string RutaBackUp = rute + "backUp";
        int ret = mkdir( RutaIndice.c_str(),0777 );
        if ( ret == 0 )
        {
            std::string rutaMet = RutaIndice + "/" + "indice";
            std::ofstream met(rutaMet.c_str());
            met.close();
        }
        ret = mkdir( RutaBackUp.c_str(),0777 );
    }
}
void PestaniaConfCliente::on_boton_act_clickeado()
{
    this->VolcarFile();
    if ( this->vc() )
    {
        this->crearCarp();
        Gtk::MessageDialog dialog( (Gtk::Window&)(*this->get_toplevel()) , "Archivo de configuracion actualizado" );
        dialog.set_secondary_text( "Proceda a comunicarse con el server" );
        dialog.run();
    }
    else
    {
        Gtk::MessageDialog dialog( (Gtk::Window&)(*this->get_toplevel()) , "Ojo!" );
        dialog.set_secondary_text( "Faltan llenar campos." );
        dialog.run();
    }
}

void PestaniaConfCliente::on_boton_conect_clickeado()
{
    //ACA SE LLAMA AL PROGRAMA CLIENTE EN SI...
    if ( this->vc() )
    {
        int ret = system("./client1");
        std::cout << ret << std::endl;
        Gtk::MessageDialog dialog( (Gtk::Window&)(*this->get_toplevel()) , "Conexión cerrada" );
        dialog.set_secondary_text( "Puede cerrar la aplicación." );
        dialog.run();
    }
    else
    {
        Gtk::MessageDialog dialog( (Gtk::Window&)(*this->get_toplevel()) , "Ojo!" );
        dialog.set_secondary_text( "Faltan llenar campos." );
        dialog.run();
    }
}

void PestaniaConfCliente::on_boton2_clickeado()
{
    std::string Key = this->Entry_Key.get_text();
    std::string Value = "";
    if ( this->abmPro.SeEncuentra(Key) )
    {
        this->Entry_Key.delete_text(0,this->Entry_Key.get_text_length());
        this->Entry_Value.delete_text(0,this->Entry_Value.get_text_length());
        this->abmPro.Escribir(Key,Value);
    }
    else
    {
    Gtk::MessageDialog dialog( (Gtk::Window&)(*this->get_toplevel()) , "Mal!" );
    dialog.set_secondary_text( "Parametro incorrecto" );
    dialog.run();
    }
    this->Entry_Key.delete_text(0,this->Entry_Key.get_text_length());
    this->Entry_Value.delete_text(0,this->Entry_Value.get_text_length());
    this->VistaPro.ActualizarVista();
}

void PestaniaConfCliente::on_boton1_clickeado()
{
    std::string Key = this->Entry_Key.get_text();
    if ( this->abmPro.SeEncuentra(Key) )
    {
        std::string Value = this->Entry_Value.get_text() ;
        if ( Key == "DirectorioAsincronizar" )
        {
            if ( (Value.find_last_of("/")) != (Value.size()-1))
            {
                Value.append("/");
            }
        }
        this->abmPro.Escribir(Key,Value);
    }
    else
    {
        Gtk::MessageDialog dialog( (Gtk::Window&)(*this->get_toplevel()) , "Mal!" );
        dialog.set_secondary_text( "Parametro incorrecto" );
        dialog.run();
    }
    this->Entry_Key.delete_text(0,this->Entry_Key.get_text_length());
    this->Entry_Value.delete_text(0,this->Entry_Value.get_text_length());
    this->VistaPro.ActualizarVista();
}

